package com.school.system.service;

import com.school.system.entity.SchoolClass;
import com.school.system.repository.SchoolClassRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 班级服务类
 * 
 * @author 鹿与兔
 * @since 2025-05-22
 */
@Service
public class SchoolClassService {

    @Autowired
    private SchoolClassRepository schoolClassRepository;

    /**
     * 分页查询班级列表
     */
    public Page<SchoolClass> getClassPage(Pageable pageable) {
        return schoolClassRepository.findAll(pageable);
    }

    /**
     * 获取所有班级列表
     */
    public List<SchoolClass> getAllClasses() {
        return schoolClassRepository.findAll();
    }

    /**
     * 根据ID获取班级信息
     */
    public SchoolClass getClassById(Long id) {
        Optional<SchoolClass> schoolClass = schoolClassRepository.findById(id);
        return schoolClass.orElse(null);
    }

    /**
     * 根据班级名称获取班级信息
     */
    public SchoolClass getClassByClassName(String className) {
        return schoolClassRepository.findByClassName(className);
    }

    /**
     * 添加班级
     */
    public SchoolClass addClass(SchoolClass schoolClass) {
        schoolClass.setCreateTime(LocalDateTime.now());
        schoolClass.setUpdateTime(LocalDateTime.now());
        schoolClass.setDeleted(0);
        return schoolClassRepository.save(schoolClass);
    }

    /**
     * 更新班级信息
     */
    public SchoolClass updateClass(SchoolClass schoolClass) {
        schoolClass.setUpdateTime(LocalDateTime.now());
        return schoolClassRepository.save(schoolClass);
    }

    /**
     * 删除班级
     */
    public void deleteClass(Long id) {
        schoolClassRepository.deleteById(id);
    }

    /**
     * 根据年级获取班级列表
     */
    public List<SchoolClass> getClassesByGrade(String grade) {
        return schoolClassRepository.findByGrade(grade);
    }

    /**
     * 根据专业获取班级列表
     */
    public List<SchoolClass> getClassesByMajor(String major) {
        return schoolClassRepository.findByMajor(major);
    }

    /**
     * 根据班级名称搜索班级
     */
    public List<SchoolClass> searchClassesByName(String className) {
        return schoolClassRepository.findByClassNameContaining(className);
    }
}
